#!/opt/bin/env python

"""
- Remember to change the above line to fit your environment
- Python >= 2.5 is required
- Read the LICENSE file before redistributing/modifying this software
"""

import sigma

class Covering:
    """
    This defines a simple indexed clique covering
    - spec must be: a,c;a,b,d;e
    """
    covering = {}
    size = 0
    spec = None
    sigma = None
    density = 0.0
    alpha = 0

    def __init__(self, spec, sigma):
        counter, cover = 0, {}
        self.spec = spec
        self.size = len(self.spec.split(';'))
        
        for clique in self.spec.split(';'):
            for symbol in clique.split(','):
                if symbol in cover.keys():
                    cover[symbol].append(counter)
                else:
                    cover[symbol] = [counter]
            counter += 1
        self.covering = cover

        self.sigma = sigma
        self.density = float(len(self)) / float(len(self.sigma))
        self.alpha = max([len(s.split(',')) for s in self.spec.split(';')])

    def __repr__(self):
        return str(self.spec)

    def __len__(self):
        return self.size

    def has_dependences(self, symbol, left, right):
        return left[self.covering[symbol]] != right[self.covering[symbol]]

    def cliques_of(self, symbol):
        return list(self.covering[symbol])

    def clique(self, idx):
        ret = []
        for symbol in self.covering.keys():
            if idx in self.covering[symbol]:
                ret.append(symbol)
        return ret

    def dotit(self, program = 'dot', file = 'covering.png'):
        import string
        dotcode = 'digraph structs {rankdir=LR;\n'
        for clique in self.spec.split(';'):
            dotcode += 'node ' + clique.replace(',','') + ' [ shape=record, label = "{' + clique.replace(',', '|') + '}" ];\n'
        dotcode += '}'

        if file is not None:
            import pydot
            pydot.graph_from_dot_data(dotcode).write_png(file, prog = program)

        return dotcode


def main():
    pass

"""
Invocation
"""
if __name__ == '__main__':
    main()
